import { CreepHelper } from "../../utils/creepHelper";
import {LogHelper} from "../../utils/logHelper";

class HatcherTask implements ICreepTask {
  public run(creep: Creep): void {
    if(creep.memory.working){

      // 找到最近的未满能量存储设施
      let target:any = CreepHelper.findNotFullExtension(creep);

      if(creep.room.find(FIND_MY_SPAWNS)[0].store.getFreeCapacity(RESOURCE_ENERGY) > 0){
        // 优先搬运到SPAWNS
        target = creep.room.find(FIND_MY_SPAWNS)[0];
      }

      if (target) {
        // 如果有未满能量存储设施，就将能量转移到它
        let result = creep.transfer(target, RESOURCE_ENERGY);
        if (result === ERR_NOT_IN_RANGE) {
          creep.go(target);
        }
        else if(result === ERR_NOT_ENOUGH_RESOURCES){
          creep.memory.working = false;
        }
      }

    }
    else {
      //如果拾取未满最大容量则继续拾取
      CreepHelper.creepPickup(creep);
    }
  }
}

export { HatcherTask };